// pages/addOrbit/addOrbit.js
const api = require('../../config/config.js');
const chooseLocation = requirePlugin('chooseLocation');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    des: "",
    lat: -1,
    lng: -1,
    date: "",
    city: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var location = chooseLocation.getLocation();
    if (location != null) {
      that.setData({
        des: location.name,
        city: location.city,
        lat: location.latitude,
        lng: location.longitude
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title:"疫轨迹，了解感染者出行活动，发现自身感染风险",
      imageUrl:"/images/share.png",
      path:"/pages/sufferer/travel/travel",
    }
  },

  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },

  getMyLocation: function (e) {
    wx.navigateTo({
      url: 'plugin://chooseLocation/index?key=' + api.mapKey + '&referer=' + api.referer
    });
  },

  formSubmit: function (e) {
    var that = this;
    var data = e.detail.value;
    var result = [];
    if (data.des == "" || data.date == "") {
      wx.showToast({
        title: '不能为空',
        icon: 'none',
        duration: 1500
      })
    } else {
      wx.showLoading({
        title: '数据记录中',
      });
      wx.cloud.callFunction({
        name: 'addOrbit',
        data: {
          des: data.des,
          date: data.date,
          city:data.city,
          note:data.note,
          longitude: data.lng*1,
          latitude:data.lat*1,
        },
        success: function (res) {
          wx.hideLoading();
          app.globalData.getOrbit=true;
          if (res.result.dangerous) {
            wx.showModal({
              title: '存在感染风险！！！！',
              content: '请到“我的出行”中了解详情',
              showCancel: false,
              confirmText: '确定',
            });
          } else {
            wx.showModal({
              title: '添加完成',
              content: '请到“我的出行”中了解详情',
              showCancel: false,
              confirmText: '确定',
            });
          }
        },
        fail: err => {
          wx.hideLoading();
          wx.showToast({
            icon: 'none',
            title: '添加失败'
          })
        }

      })
    }
  },
})